import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
} from '@nestjs/common';
import { ApiService } from './api.service';
import { CreateApiDto } from './dto/create-api.dto';
import { UpdateApiDto } from './dto/update-api.dto';
import CryptoUtil from '../utils/encrypt';

@Controller('api')
export class ApiController {
  constructor(private readonly apiService: ApiService) {}
  @Post('signup')
  signup(@Body() createApiDto: CreateApiDto) {
    const { password } = createApiDto;
    const CryptoMap = new CryptoUtil('byte-movie', 'douyin');
    const result = CryptoMap.decrypt(password);
    const listMap = ['0961', '0962', '0963', '0964'];
    if (listMap.includes(result)) {
      return {
        code: 200,
        msg: '注册成功',
        data: {
          ...createApiDto,
          password: '******',
        },
      };
    }
    return {
      code: 200,
      msg: '注册失败',
      data: createApiDto,
    };
  }

  @Post()
  create(@Body() createApiDto: CreateApiDto) {
    return this.apiService.create(createApiDto);
  }

  @Get()
  findAll() {
    return this.apiService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.apiService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateApiDto: UpdateApiDto) {
    return this.apiService.update(+id, updateApiDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.apiService.remove(+id);
  }
}
